#include "FWGraphic.h"


FWGraphic::FWGraphic(FWGame* fwGame)
{
	this->_gameObject = fwGame;
	fontFace = FONT_FACE;
}

FWGraphic::~FWGraphic(void)
{
}

D3DXVECTOR2 FWGraphic::_worldPositionToViewport(D3DXVECTOR2 world,D3DXVECTOR2 center)
{
	vp_pos;
	_world.x = world.x + center.x ;
	_world.y = world.y +  center.y;
	_world.z = 0;
	D3DXVec3Transform(&vp_pos,&_world,this->_tranform);
	world_viewport.x = vp_pos.x;
	world_viewport.y = vp_pos.y;
	return world_viewport;
}

bool FWGraphic::init()
{
	HRESULT hr = D3DXCreateSprite(_gameObject->d3ddev,&this->_sprite);
	if(FAILED(hr)) return false;
	backbuffer = NULL;
	return true;	
}

void FWGraphic::begin()
{
	this->_gameObject->d3ddev->BeginScene();
	this->_sprite->Begin(D3DXSPRITE_ALPHABLEND);
	this->_tranform = NULL;
}

void FWGraphic::begin(D3DXMATRIX* tranform)
{

	this->_gameObject->d3ddev->BeginScene();
	this->_sprite->Begin(D3DXSPRITE_ALPHABLEND);
	this->_tranform = tranform;
}

void FWGraphic::end()
{
	this->_sprite->End();
	this->_gameObject->d3ddev->EndScene();
	this->_gameObject->d3ddev->Present(NULL,NULL,NULL,NULL);	
}

void FWGraphic::clear(D3DCOLOR color)
{
	this->_gameObject->d3ddev->Clear(1,NULL,D3DCLEAR_TARGET,color,1,NULL);
}

void FWGraphic::nDraw(FWTexture* texture,D3DXVECTOR2 position,D3DXVECTOR2 center, D3DCOLOR color)
{
	nDraw_center.x = center.x;
	nDraw_center.y = center.y;
	nDraw_center.z = 0;
	nDraw_position.x = position.x;
	nDraw_position.y = position.y;
	nDraw_position.z = 0;
	this->_sprite->Draw(texture->getTexture(),NULL,&nDraw_center,&nDraw_position,color);
}

void FWGraphic::nDraw(FWTexture* texture,RECT destRect,D3DXVECTOR2 center, D3DCOLOR color)
{
	//tim do dan
	float xScale = (destRect.right - destRect.left) / texture->getWidth();
	float yScale = (destRect.bottom - destRect.top) / texture->getHeight();
	//cac ma tran ve ti le
	D3DXMATRIX result,currentState;
	//tao ma tran scale
	D3DXMatrixScaling(&result,xScale,yScale,0);	
	//tao position
	nDraw_position.x = destRect.left / xScale;
	nDraw_position.y = destRect.top / yScale;
	nDraw_position.z = 0;
	//tao diem giua
	nDraw_center.x = center.x / xScale;
	nDraw_center.y = center.y / yScale;
	nDraw_center.z = 0;
	//ap dung cac transform
	_sprite->GetTransform(&currentState);
	_sprite->SetTransform(&result);
	_sprite->Draw(texture->getTexture(),NULL,&nDraw_center,&nDraw_position,color);
	//khoi phuc cac transforms
	_sprite->SetTransform(&currentState);

}

void FWGraphic::nDraw(FWTexture* texture,RECT sourRect,RECT destRect,D3DXVECTOR2 center, D3DCOLOR color)
{

	//tim do dan
	float xScale = (destRect.right - destRect.left) / (sourRect.right - sourRect.left);
	float yScale = (destRect.bottom - destRect.top) / (sourRect.bottom - sourRect.top);
	//cac ma tran ve ti le
	D3DXMATRIX result,currentState;
	//tao ma tran scale
	D3DXMatrixScaling(&result,xScale,yScale,0);	
	//tao position
	nDraw_position.x = destRect.left / xScale;
	nDraw_position.y =  destRect.top / yScale;
	nDraw_position.z = 0;
	//tao diem giua
	nDraw_center.x = center.x / xScale;
	nDraw_center.y = center.y / yScale;
	nDraw_center.z = 0;
	//ap dung cac transform
	_sprite->GetTransform(&currentState);
	_sprite->SetTransform(&result);
	_sprite->Draw(texture->getTexture(),&sourRect,&nDraw_center,&nDraw_position,color);
	//khoi phuc cac transforms
	_sprite->SetTransform(&currentState);


}


















void FWGraphic::tDraw(FWTexture* texture,D3DXVECTOR2 position,D3DXVECTOR2 center, D3DCOLOR color)
{
	tDraw_vp_pos = _worldPositionToViewport(position,center);
	this->nDraw(texture,tDraw_vp_pos,center,color);
}

void FWGraphic::tDraw(FWTexture* texture,RECT destRect,D3DXVECTOR2 center, D3DCOLOR color)
{
	tDraw_vec2_tmp.x = destRect.left;
	tDraw_vec2_tmp.y = destRect.top;
	tDraw_vp_pos = this->_worldPositionToViewport(tDraw_vec2_tmp,center);
	int _width = destRect.right - destRect.left;
	int _height = destRect.bottom - destRect.top;
	RECT tdes;
	tdes.left = tDraw_vp_pos.x;
	tdes.top = tDraw_vp_pos.y;
	tdes.right = tdes.left + _width;
	tdes.bottom = tdes.top + _height;
	this->nDraw(texture,tdes,center,color);
}


void FWGraphic::tDraw(FWTexture* texture,RECT sourRect,RECT destRect,D3DXVECTOR2 center, D3DCOLOR color)
{
	tDraw_vec2_tmp.x = destRect.left;
	tDraw_vec2_tmp.y = destRect.top;
	tDraw_vp_pos = this->_worldPositionToViewport(tDraw_vec2_tmp,center);
	int _width = destRect.right - destRect.left;
	int _height = destRect.bottom - destRect.top;
	RECT tdes;
	tdes.left = tDraw_vp_pos.x;
	tdes.top = tDraw_vp_pos.y;
	tdes.right = tdes.left + _width;
	tdes.bottom = tdes.top + _height;
	this->nDraw(texture,sourRect,tdes,center,color);
}


bool FWGraphic::initFont()
{
	AddFontResourceEx("Resource//FONT//impact.TTF",FR_PRIVATE,0);
	ZeroMemory(&fontDesc, sizeof(D3DXFONT_DESC));
	fontDesc.Height = fontSize.x;
	fontDesc.Width = fontSize.y;
	fontDesc.Weight = 500;
	fontDesc.MipLevels = D3DX_DEFAULT;
	fontDesc.Italic = false;
	fontDesc.CharSet = 0;
	fontDesc.OutputPrecision = 0;
	fontDesc.Quality = 0;
	fontDesc.PitchAndFamily = 0;
	strcpy_s(fontDesc.FaceName,strlen(fontFace)+1, this->fontFace);
	if(FAILED(D3DXCreateFontIndirect(this->_gameObject->d3ddev, &fontDesc, &font)))
		return false;
	return true;	
}
void FWGraphic::sDraw(char* text, D3DXVECTOR2 pos,D3DCOLOR color)
{
	int index;	
	RECT s_rect;	

	float str_width = strlen(text)*fontSize.x;
	float str_height =  fontSize.y;

	D3DXVECTOR2 toa_do_man_anh = _worldPositionToViewport(pos,D3DXVECTOR2(str_width / 2.0f,str_height / 2.0f));
	s_rect.left = toa_do_man_anh.x - str_width / 2.0f;
	s_rect.top = toa_do_man_anh.y  - str_height / 2.0f;
	s_rect.right= s_rect.left + str_width;
	s_rect.bottom =s_rect.top + strlen(text)*fontSize.y;
	font->DrawText(_sprite,
		text,
		-1, // so ki tu trong chuoi, -1 ->khong gioi han
		&s_rect,
		DT_TOP|DT_LEFT,
		color);
}

void FWGraphic::SetFontSize(D3DXVECTOR2 size)
{
	this->fontSize = size;
	if(!this->initFont())
		MessageBox(NULL,"Khong the sua kich thuoc chu",NULL,NULL);
}